package com.tb.market;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.tb.coremodel.datamodel.http.entities.MarketData;

import com.tb.common.base.ARouterPath;
import com.tb.common.base.BaseActivity;
import com.tb.coremodel.viewmodel.BaseViewModel;
import com.tb.coremodel.viewmodel.DynamicMarketViewModel;
import com.tb.coremodel.viewmodel.ViewModelProviders;
import com.tb.market.databinding.ActivityMarketBinding;

/**
 * Created by Clark on 2022/02/28.
 */
@Route(path = ARouterPath.DynaMarketListAty)
public class ActivityDynamicMarket extends BaseActivity {
    @Autowired(name = "fullUrl")
    public String fullUrl;
    MarketAdapter marketAdapter;
    ActivityMarketBinding activityGirlsBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("动态行情");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        activityGirlsBinding = DataBindingUtil.setContentView(ActivityDynamicMarket.this, R.layout.activity_market);
        //inject需要注入后才可以读取到携带过来的参数
        ARouter.getInstance().inject(this);

        Log.i("clark", "fullUrl-->"+fullUrl);
        if(TextUtils.isEmpty(fullUrl)){
            return;
        }

//        DynamicGirlsViewModel dynamicGirlsViewModel = new DynamicGirlsViewModel(ActivityDynamicGirls.this.getApplication(), fullUrl);
        DynamicMarketViewModel dynamicGirlsViewModel =
                ViewModelProviders.of(ActivityDynamicMarket.this).get(DynamicMarketViewModel.class);
        dynamicGirlsViewModel.loadData(fullUrl);
                marketAdapter = new MarketAdapter(marketItemClickCallback);
        activityGirlsBinding.setRecyclerAdapter(marketAdapter);
        subscribeToModel(dynamicGirlsViewModel);

    }

    MarketItemClickCallback marketItemClickCallback = new MarketItemClickCallback() {
        @Override
        public void onClick(MarketData.ResultsBean fuliItem) {
            Toast.makeText(ActivityDynamicMarket.this, fuliItem.getDesc(), Toast.LENGTH_SHORT).show();
        }
    };

    /**
     * 订阅数据变化来刷新UI
     * @param model
     */
    private void subscribeToModel(final BaseViewModel model){
        //观察数据变化来刷新UI
        model.getLiveObservableData().observe(this, new Observer<MarketData>() {
            @Override
            public void onChanged(@Nullable MarketData marketData) {
                Log.i("clark", "subscribeToModel onChanged onChanged");
                model.setUiObservableData(marketData);
                marketAdapter.setMarketList(marketData.getResults());
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
